package zuoye.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ResultUtil {
	static {// 静态块
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();

		}
	}

	private ResultUtil() {
	}

	final static String url = "jdbc:mysql://localhost:3306/daywork?useUnicode=true&characterEncoding=utf8";

	public static Connection getConnection() {
		try {
			return DriverManager.getConnection(url, "root", "2129");
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return null;
	}

	public static void closeAll(ResultSet r, Statement s, Connection c) {
		close(r);
		close(s);
		close(c);
	}

	public static void close(ResultSet r) {
		if (r != null)
			try {
				r.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
	}

	public static void close(Statement r) {
		if (r != null)
			try {
				r.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
	}

	public static void close(Connection r) {
		if (r != null)
			try {
				r.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
	}

	/**
	 * 查操作
	 * 
	 * @param sql
	 * @param args
	 * @return
	 */
	public static List<Result> search(String sql, Object... args) {
		Connection con = getConnection();
		PreparedStatement ps = null;
		List<Result> list = new ArrayList<Result>();
		ResultSet rs = null;
		try {
			ps = con.prepareStatement(sql);

			for (int i = 0; i < args.length; i++) {
				// 有一些属性需要单独处理
				if (args[i] instanceof java.util.Date) {
					java.util.Date d = (Date) args[i];
					ps.setDate(i + 1,
							d == null ? null : new java.sql.Date(d.getTime()));
					continue;
				}
				ps.setObject(i + 1, args[i]);
			}
			rs = ps.executeQuery();
			while (rs.next()) {
				Result s = new Result();
				s.setId(rs.getInt("id"));
				s.setExam_date(rs.getDate("exam_date"));
				s.setSb_id(rs.getInt("sb_id"));
				s.setScore(rs.getDouble("score"));
				s.setSt_id(rs.getInt("st_id"));
				list.add(s);// !!!!!!!!!!!!!!!
			}
		} catch (SQLException e) {
			// 忽略异常
			e.printStackTrace();
		} finally {
			closeAll(rs, ps, con);
		}
		return list;
	}

	/**
	 * 增删改操作
	 * 
	 * @param sql
	 * @param args
	 * @return
	 */
	public static int executeUpdate(String sql, Object... args) {
		Connection con = getConnection();
		PreparedStatement ps = null;
		try {
			ps = con.prepareStatement(sql);
			for (int i = 0; i < args.length; i++) {
				// 有一些属性需要单独处理
				if (args[i] instanceof java.util.Date) {
					java.util.Date d = (Date) args[i];
					ps.setDate(i + 1,
							d == null ? null : new java.sql.Date(d.getTime()));
					continue;
				}
				ps.setObject(i + 1, args[i]);
			}
			return ps.executeUpdate();
		} catch (SQLException e) {
			// 忽略异常
			e.printStackTrace();
		} finally {
			close(ps);
			close(con);
		}
		return 0;
	}

}
